﻿using InkjetControl.bean;
using InkjetControl.codec;
using InkjetControl.events;
using InkjetControl.NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace InkjetControl.model
{
    public class PrintParamsModel : ViewModelBase
    {

        /// <summary>
        /// 正向字体宽度->喷印速度
        /// </summary>
        private int _fontWidth;
        private int _fontHeight;
        private int _fontBold;
        private int _printMode;
        private int _verticalLattice;
        private long _printSpacing;
        private int _printTimes;
        private int _upsideDown;
        private int _syncMode;
        private int _syncColumnSpacing;
        private long _printDelay;
        private long _reversePrintDelay;
        private int _reverseFontWidth;
        private int _reverseFontHeight;
        private int _electricEye1Direction;
        private int _electricEye2Direction;
        private int _electricEye1Level;
        private int _electricEye2Level;
        private int _reverseUpsideDown;
        private int _reversePrintMode;
        private int _reversePrintTimes;
        private int _reversePrintSpacing;

        private readonly string DeviceId;

        public bool IsChanged { get; set; }


        public PrintParamsModel(string deviceId)
        {
            DeviceId = deviceId;
            FontHeightComboVirtualization = new List<int>(Enumerable.Range(1, 10));
            FontBlodComboVirtualization = new List<int>(Enumerable.Range(0, 9));

            _electricEye1Level = 2;
            _electricEye2Level = 2;

            FontHeight = FontHeightComboVirtualization.First();
            ReverseFontHeight = FontHeightComboVirtualization.First();

            FontBold = FontBlodComboVirtualization.First();
            IsChanged = true;
        }

        /// <summary>
        /// 喷印计数
        /// </summary>
        private long _printCount;
        /// <summary>
        /// 喷印统计总数
        /// </summary>
        private long _printTotalCount;

        /// <summary>
        /// 正向字体宽度->喷印速度
        /// </summary>
        public int FontWidth
        {
            get => _fontWidth;
            set
            {
                if (SetProperty(ref _fontWidth, value) && IsChanged)
                {
                    SendParams(DeviceId, "fontWidth", ref _fontWidth);
                }
            }
        }

        /// <summary>
        /// 正向字体高度
        /// </summary>
        public int FontHeight
        {
            get => _fontHeight;
            set
            {
                if (SetProperty(ref _fontHeight, value) && IsChanged)
                {
                    SendParams(DeviceId, "fontHeight", ref _fontHeight);
                }
            }
        }

        /// <summary>
        /// 正向字体加粗
        /// </summary>
        public int FontBold
        {
            get => _fontBold;
            set
            {
                if (SetProperty(ref _fontBold, value) && IsChanged)
                {
                    SendParams(DeviceId, "fontBold", ref _fontBold);
                }
            }
        }

        /// <summary>
        /// 正向喷印方式: 
        /// 0表示计数喷印
        /// 1表示连续喷印
        /// </summary>
        public int PrintMode
        {
            get => _printMode;
            set
            {
                if (SetProperty(ref _printMode, value) && IsChanged)
                {
                    SendParams(DeviceId, "printMode", ref _printMode);
                }
            }
        }

        /// <summary>
        /// 纵向点阵
        /// </summary>
        public int VerticalLattice
        {
            get => _verticalLattice;
            set
            {
                if (SetProperty(ref _verticalLattice, value) && IsChanged)
                {
                    SendParams(DeviceId, "verticalLattice", ref _verticalLattice);
                }

            }
        }

        /// <summary>
        /// 正向喷印间距
        /// </summary>
        public long PrintSpacing
        {
            get => _printSpacing;
            set
            {
                if (SetProperty(ref _printSpacing, value) && IsChanged)
                {
                    SendParams(DeviceId, "printSpacing", ref _printSpacing);
                }

            }
        }

        /// <summary>
        /// 正向喷印次数
        /// </summary>
        public int PrintTimes
        {
            get => _printTimes;
            set
            {
                if (SetProperty(ref _printTimes, value) && IsChanged)
                {
                    SendParams(DeviceId, "printTimes", ref _printTimes);
                }
            }
        }

        /// <summary>
        /// 上下颠倒<br/>
        /// 0:表示不颠倒<br/>
        /// 1:表示上下颠倒<br/>
        /// </summary>
        public int UpsideDown
        {
            get => _upsideDown;
            set
            {
                if (SetProperty(ref _upsideDown, value) && IsChanged)
                {
                    SendParams(DeviceId, "upsideDown", ref _upsideDown);
                }
            }
        }

        /// <summary>
        /// 同步器状态
        /// 0表示不使用同步器
        /// 1表示使用同步器
        /// </summary>
        public int SyncMode
        {
            get => _syncMode;
            set
            {
                if (SetProperty(ref _syncMode, value) && IsChanged)
                {
                    SendParams(DeviceId, "syncMode", ref _syncMode);
                }
            }
        }

        /// <summary>
        /// 同步列间距
        /// </summary>
        public int SyncColumnSpacing
        {
            get => _syncColumnSpacing;
            set
            {
                if (SetProperty(ref _syncColumnSpacing, value) && IsChanged)
                {
                    SendParams(DeviceId, "syncColumnSpacing", ref _syncColumnSpacing);
                }

            }
        }

        /// <summary>
        /// 正向喷印延时
        /// </summary>
        public long PrintDelay
        {
            get => _printDelay;
            set
            {

                if (SetProperty(ref _printDelay, value) && IsChanged)
                {
                    SendParams(DeviceId, "printDelay", ref _printDelay);
                }
            }
        }

        /// <summary>
        /// 反向喷印延时
        /// </summary>
        public long ReversePrintDelay
        {
            get => _reversePrintDelay;
            set
            {
                if (SetProperty(ref _reversePrintDelay, value) && IsChanged)
                {
                    SendParams(DeviceId, "reversePrintDelay", ref _reversePrintDelay);
                }

            }
        }

        /// <summary>
        /// 反向字体宽度->反向喷印速度
        /// </summary>
        public int ReverseFontWidth
        {
            get => _reverseFontWidth;
            set
            {
                if (SetProperty(ref _reverseFontWidth, value) && IsChanged)
                {
                    SendParams(DeviceId, "reverseFontWidth", ref _reverseFontWidth);
                }
            }
        }


        /// <summary>
        /// 正向字体方向
        /// 电眼1 方向:
        /// 0 没任何颠倒
        /// 1 左右颠倒
        /// 2 上下颠倒
        /// 3 上下_左右颠倒
        /// </summary>
        public int ElectricEye1Direction
        {
            get => _electricEye1Direction;
            set
            {
                if (SetProperty(ref _electricEye1Direction, value) && IsChanged)
                {
                    SendParams(DeviceId, "electricEye1Direction", ref _electricEye1Direction);
                }
            }
        }

        /// <summary>
        /// 反向字体方向
        /// 电眼2 方向:
        /// 0 没任何颠倒
        /// 1 左右颠倒
        /// 2 上下颠倒
        /// 3 上下_左右颠倒
        /// </summary>
        public int ElectricEye2Direction
        {
            get => _electricEye2Direction;
            set
            {
                if (SetProperty(ref _electricEye2Direction, value) && IsChanged)
                {
                    SendParams(DeviceId, "electricEye2Direction", ref _electricEye2Direction);
                }

            }
        }

        /// <summary>
        ///  正喷印电平状态
        ///  0低电平
        ///  1高电平
        ///  2关闭
        /// </summary>
        public int ElectricEye1Level
        {
            get => _electricEye1Level;
            set
            {
                if (SetProperty(ref _electricEye1Level, value))
                {
                    IsEnabledElectricEye1 = _electricEye1Level != 2;
                    if (IsChanged)
                    {
                        SendParams(DeviceId, "electricEye1Level", ref _electricEye1Level);
                    }
                }


            }
        }

        /// <summary>
        /// 反喷印电平状态
        /// 0低电平
        /// 1高电平
        /// 2关闭
        /// </summary>
        public int ElectricEye2Level
        {
            get => _electricEye2Level;
            set
            {

                if (SetProperty(ref _electricEye2Level, value))
                {
                    IsEnabledElectricEye2 = _electricEye2Level != 2;
                    if (IsChanged)
                    {
                        SendParams(DeviceId, "electricEye2Level", ref _electricEye2Level);
                    }
                }
            }
        }

        /// <summary>
        /// 反向字体高度
        /// </summary>
        public int ReverseFontHeight
        {
            get => _reverseFontHeight;
            set
            {
                if (SetProperty(ref _reverseFontHeight, value) && IsChanged)
                {
                    SendParams(DeviceId, "reverseFontHeight", ref _reverseFontHeight);
                }
            }
        }

        /// <summary>
        /// 反向上下颠倒<br/>
        ///  0:表示不颠倒<br/>
        ///  1:表示上下颠倒<br/>
        /// </summary>
        public int ReverseUpsideDown
        {
            get => _reverseUpsideDown;
            set
            {
                if (SetProperty(ref _reverseUpsideDown, value))
                {

                    if (IsChanged)
                    {
                        SendParams(DeviceId, "reverseUpsideDown", ref _reverseUpsideDown);
                    }
                }
            }
        }

        /// <summary>
        /// 反向喷印方式
        /// </summary>
        public int ReversePrintMode
        {
            get => _reversePrintMode;
            set
            {

                SetProperty(ref _reversePrintMode, value);

            }
        }

        /// <summary>
        /// 反向喷印次数
        /// </summary>
        public int ReversePrintTimes
        {
            get => _reversePrintTimes;
            set => SetProperty(ref _reversePrintTimes, value);
        }

        /// <summary>
        /// 反向喷印间隔
        /// </summary>
        public int ReversePrintSpacing
        {
            get => _reversePrintSpacing;
            set => SetProperty(ref _reversePrintSpacing, value);
        }


        /// <summary>
        /// 喷印计数
        /// </summary>
        public long PrintCount
        {
            get => _printCount;
            set => SetProperty(ref _printCount, value);
        }

        /// <summary>
        /// 喷印统计总数
        /// </summary>
        public long PrintTotalCount
        {
            get => _printTotalCount;
            set => SetProperty(ref _printTotalCount, value);
        }

        private bool _isEnabledElectricEye1;
        private bool _isEnabledElectricEye2;


        public bool IsEnabledElectricEye1
        {
            get => _isEnabledElectricEye1;
            set => SetProperty(ref _isEnabledElectricEye1, value);
        }
        public bool IsEnabledElectricEye2
        {
            get => _isEnabledElectricEye2;
            set => SetProperty(ref _isEnabledElectricEye2, value);
        }

        private static void SendParams<T>(string deviceId, string key, ref T value)
        {
            Message msg = new Message
            {
                MachineCode = deviceId,
                MessageType = MessageType.REQUEST,
            };

            SingleParam<T> param = new SingleParam<T>(key, value);
            PostPrintParamsBean<SingleParam<T>> postPrintParams = PostPrintParamsBean<SingleParam<T>>.GenerateParams(true, ref param);

            msg.Body = Message.GenerateMsgBody(NetCmd.SET_PRINT_PARAMS, ref postPrintParams);

            EventBus.SendMsgHandler(deviceId, msg);
        }

        public IList<int> FontHeightComboVirtualization { get; }
        public IList<int> FontBlodComboVirtualization { get; }

    }
}
